package com.wys.mall.product.service.impl;

import com.wys.common.utils.MyStrUtil;
import com.wys.mall.product.dao.SkuImagesDao;
import com.wys.mall.product.entity.SkuImagesEntity;
import com.wys.mall.product.entity.SpuInfoDescEntity;
import com.wys.mall.product.service.*;
import com.wys.mall.product.vo.SkuItemSaleAttrVo;
import com.wys.mall.product.vo.SkuItemVo;
import com.wys.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.common.utils.PageUtils;
import com.wys.common.utils.Query;

import com.wys.mall.product.dao.SkuInfoDao;
import com.wys.mall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = String.valueOf(params.get("key"));
        String brandId = String.valueOf(params.get("brandId"));
        String catelogId = String.valueOf(params.get("catelogId"));
        String min = String.valueOf(params.get("min"));
        String max = String.valueOf(params.get("max"));
        if (MyStrUtil.isNotEmpty(key)) {
            queryWrapper.and(w -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        if (MyStrUtil.isNotBlank(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        if (MyStrUtil.isNotBlank(catelogId)) {
            queryWrapper.eq("catalog_id", catelogId);
        }
        if (MyStrUtil.isNotEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        if (MyStrUtil.isNotEmpty(max)) {
            try {
                BigDecimal maxDecimal = new BigDecimal(max);
                if (maxDecimal.compareTo(BigDecimal.ZERO) == 1) {
                    queryWrapper.le("price", max);
                }
            } catch (Exception e) {
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return this.baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        //1.sku基本信息 pms_sku_info
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info = this.getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(skuInfo -> {
            //3.获取spu的销售属性组合
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(skuInfo.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(skuInfo -> {
            //5.获取spu的规格参数信息
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(skuInfo.getSpuId());
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);

        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync(skuInfo -> {
            //5.获取spu的规格参数
            List<SpuItemAttrGroupVo> spuItemAttrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId());
            skuItemVo.setGroupAttrs(spuItemAttrGroupVos);
        }, executor);

        //2.sku的图片信息 pms_sku_img
        CompletableFuture<Void> skuImagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

//        等待全部完成，返回
        CompletableFuture.allOf(infoFuture, saleAttrFuture, descFuture, attrFuture, skuImagesFuture).get();

        return skuItemVo;
    }

}